package monster.com.cvh.activity.base;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.annotation.ColorRes;
import android.support.annotation.IdRes;
import android.support.annotation.Nullable;
import android.support.annotation.StringRes;
import android.support.v7.app.AppCompatActivity;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.Toast;

import butterknife.ButterKnife;
import butterknife.Unbinder;
import monster.com.cvh.util.CustomToast;
import monster.com.cvh.util.SPUtils;
import monster.com.cvh.util.StatusBarUtil;
import monster.com.cvh.util.ToastUtils;

/**
 * @author: 13580
 * @data: 2018/3/14
 * @function:公共的activity
 */

public abstract class BaseActivity extends AppCompatActivity {
    private Unbinder mUnbinder;
    public String mTag;//输出日志，所需tag

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //锁定竖屏
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        setContentView(getContentViewResId());
        mTag = getComponentName().getShortClassName();
        mUnbinder = ButterKnife.bind(this);
        initData();
        initEvent();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mUnbinder.unbind();
    }

    /**
     * 初始化布局文件
     *
     * @return
     */
    protected abstract int getContentViewResId();

    /**
     * 初始化控件事件
     */
    protected abstract void initEvent();

    /**
     * 初始化数据
     */
    protected abstract void initData();

    /**
     * activity跳转
     *
     * @param clazz
     */
    protected void startActivity(Class<?> clazz) {
        Intent intent = new Intent(this, clazz);
        startActivity(intent);
    }

    /**
     * 绑定控件
     *
     * @param id
     * @param <T>
     * @return
     */
    protected <T extends View> T findView(@IdRes int id) {
        return (T) findViewById(id);
    }


    /**
     * 隐藏状态栏
     */
    protected void hiddenStatusBar() {
        getWindow().setFlags(
                WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN
        );
    }

    /**
     * 改变状态栏颜色
     *
     * @param color
     */
    protected void changeStatusBarColor(@ColorRes int color) {
        StatusBarUtil.setStatusBarColor(this, color);
    }

    /**
     * 调整状态栏为亮模式，这样状态栏文字颜色就为深色模式了
     */
    protected void changeStatusBarColorDark(Activity activity, @ColorRes int color) {
        StatusBarUtil.setStatusBarLightMode(activity, getResources().getColor(color));
    }

    /**
     * 禁止系统修改app字体大小
     */
    @Override
    public Resources getResources() {
        Resources res = super.getResources();
        Configuration config = new Configuration();
        config.setToDefaults();
        res.updateConfiguration(config, res.getDisplayMetrics());
        return res;
    }

    protected void showToastShort(@StringRes int res) {
        ToastUtils.setGravity(Gravity.CENTER, 0, 0);
        CustomToast.show(getResources().getString(res), Toast.LENGTH_SHORT);
    }

    protected void showToastLong(@StringRes int res) {
        ToastUtils.setGravity(Gravity.CENTER, 0, 0);
        CustomToast.show(getResources().getString(res), Toast.LENGTH_LONG);
    }

    protected void showToastShort(String content) {
        ToastUtils.setGravity(Gravity.CENTER, 0, 0);
        CustomToast.show(content, Toast.LENGTH_SHORT);
    }

    protected void showToastLong(String content) {
        ToastUtils.setGravity(Gravity.CENTER, 0, 0);
        CustomToast.show(content, Toast.LENGTH_LONG);
    }

    /**
     * 检查登录
     * @return
     */
    public boolean isLogined(){
        return !SPUtils.getString(this, "token", "").equals("");
    }

}
